using System;

namespace ActivUFRJ.Classes.Geral
{
    public class DBInstituicao : Connection.MySQL
    {
        public DBInstituicao() : base(ref Connection.DBConnection.myConnection)
        {

        }

        private void FillInstituicao(Instituicao instituicao)
        {
            instituicao.ID = Convert.ToInt32(this.TableGetValue("INS_ID"));
            instituicao.Descricao = Convert.ToString(this.TableGetValue("INS_Descricao"));
            instituicao.Sigla = Convert.ToString(this.TableGetValue("INS_SIGLA"));
            instituicao.IdEstado = Convert.ToInt32(this.TableGetValue("EST_ID"));
        }

        internal Instituicao[] GetListaInstituicao()
        {
            try
            {
                string SQL = "SELECT * from INSTITUICOES order by INS_DESCRICAO";

                this.SetSQL(SQL);

                int i = 0;
                Instituicao[] listaInstituicoes = new Instituicao[this.GetRowCount()];

                while (i < this.GetRowCount())
                {
                    Instituicao novaInst = new Instituicao();
                    this.FillInstituicao(novaInst);
                    listaInstituicoes[i] = novaInst;

                    i++;
                    this.TableMoveNext();
                }

                return listaInstituicoes;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        internal System.Data.DataTable GetListaInstituicaoDataTable()
        {
            string SQL = "SELECT * from INSTITUICOES order by INS_DESCRICAO";
            this.SetSQL(SQL);

            if (this.GetRowCount() > 0) return this.ReturnTable();
            else return null;
        }

        internal bool CarregarInstituicao(Instituicao instituicao)
        {
            try
            {
                string SQL = "SELECT * from INSTITUICOES where INS_ID = " + instituicao.ID.ToString();

                this.SetSQL(SQL);
                if (!this.IsTableOver())
                {
                    FillInstituicao(instituicao);
                    return true;
                }
                else return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        internal System.Data.DataTable GetListaInstituicoesDT()
        {
            try
            {
                string SQL = "SELECT * from INSTITUICOES";

                this.SetSQL(SQL);

                this.SetSQL(SQL);
                return this.ReturnTable();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

    }
}
